/**
 * Created by Alice on 2016/9/23 0023.
 */
var ec=echarts;
$(function () {
    $("#loading").hide();
    getCourtsByPid();
    initChart();
    initGuanxi();
});
//var markPointData = [];
//var markPointLocation = {};
//var categoryData = [];
//var categoryLinkData = [];
var tempIDandName = {};

var datas=[];
var sourceTarget=[];
var classification=[];
var markPoints = [];

function parseCourtList(data){
    for(var i=0;i<data.length;i++){
        tempIDandName[data[i].courtid] =  data[i].courtname;
    }
    for(var i=0;i<data.length;i++){
    	 var categoryTemp = {};
//        var temp = {};
//        temp.name = data[i].courtname;
//        temp.value = '';
//        //所有法院的名字  name:'' ,value:''
//        markPointData.push(temp);					
//        //法院名和其坐标信息
//        markPointLocation[data[i].courtname] = [data[i].lng,data[i].lat];  
        categoryTemp.name=data[i].courtname;
        categoryTemp.coord=[data[i].lng,data[i].lat,data[i].courtname];
    	  markPoints.push(categoryTemp);
        var markPointMap={};
//       
//        categoryTemp.category = data[i].level == 1 ? 0 : data[i].level == 2 ? 1 : 2;
//        categoryTemp.name = data[i].courtname;
//        categoryTemp.value = data[i].level == 1 ? 9 : data[i].level == 2 ? 6 : 3;
//        //法院信息，"category":0,"name":"福建省高级人民法院","value":9
//        categoryData.push(categoryTemp);					
//        var categoryLinkTemp = {};
//        categoryLinkTemp.source = data[i].courtname;
//        categoryLinkTemp.target = tempIDandName[data[i].pid];
//        categoryLinkTemp.weight = data[i].level;
//        //关联信息"source":"福建省高级人民法院","weight":1
//        categoryLinkData.push(categoryLinkTemp);
        
        //Echarts3.0
     	var datasMap={};
        datasMap.name=data[i].courtname;
        datasMap.value= data[i].level == 1 ? 9 : data[i].level == 2 ? 6 : 3;
        datasMap.symbolSize=data[i].level == 1 ? 60 : data[i].level == 2 ? 40 : 20;
       if(data[i].level == 2){
    	   datasMap.category  = data[i].courtname;
       }else{
    	   datasMap.category  =tempIDandName[data[i].pid];
       }
        datasMap.draggable ='true';
        //data数据
        datas.push(datasMap);
        var sourceTargetMap={};
        sourceTargetMap.source=data[i].courtname;
        sourceTargetMap.target=tempIDandName[data[i].pid];
        //连接信息
        sourceTarget.push(sourceTargetMap);
        //分类信息
        if(data[i].level == 2){
        	var categoriesMap={};
        	categoriesMap.name=data[i].courtname;
            classification.push(categoriesMap);
        }
    }
    
//     console.log(JSON.stringify(markPointData));
//     console.log(JSON.stringify(markPointLocation));
//     console.log(JSON.stringify(categoryData));
//     console.log(JSON.stringify(categoryLinkData));
    
//       console.log(JSON.stringify(datas));
//       console.log(JSON.stringify(sourceTarget));
//       console.log(JSON.stringify(classification));
//       console.log(JSON.stringify(markPoints));
}
function getCourtsByPid() {
    $.ajax({
        url : r.base_path + "index/getAllCourts",
        type : "post",
        data: {
            courtId: r.court_id
        },
        dataType : "json",
        async:false,
        success : function(data) {
            parseCourtList(data);
        }
    });
}
/**
 * 初始化图表
 */
function initChart(){
	 $.getJSON(r.base_path+'assets/plugins/echart/json/'+r.court_id.charAt(0)+'00'+'.json', function (data) {
	        echarts.registerMap('map', data);
	        var myChart = ec.init(document.getElementById('chart_map'));
	        myChart.setOption({
	        		name:"法院分布",
	        	    series: [{
	                type: 'map',
	                map: 'map',
	                roam:true,
	              //图形上的文本标签，可用于说明图形的一些数据信息
	                label: {
	                    normal: {
	                        show: true
	                    },
	                    emphasis: {
	                        show: true
	                    }
	                },
	                itemStyle: {
	                    //normal 是图形在默认状态下的样式；
	                        normal: {
	                            show: true,
	                            areaColor:"#33AEE5",
	                            borderColor:"#FCFCFC",
	                            borderWidth:"1",
	                            color:"red"
	                        }},
//	                selectedMode : 'multiple',
	                markPoint:{
	                	 label: {
		                        normal: {
		                            show: false,
		                            formatter: function(d) {	//标注的显示信息
		                                return d.name
		                            }
		                        }
		                    },
	                    symbolSize:10,
	                    hoverable:false,
	                    symbol:'pin',  //标记点的样式
	                    showSymbol:true,
	                    itemStyle: {
	                    	   normal:{	//普通样式
	                             label:{
	                                 show:true,
	                                 textStyle: {
	                                     color: '#bdebff',
	                                     fontSize:13
	                                 }
	                             },
	                             areaStyle:{
	                                 color:'#33AEE5' //设置地图默认区域色
	                             },
	                             textStyle: {
	                                 color: '#222',
	                                 fontSize: 20
	                             },
	                             color: {
	                            	    type: 'linear',
	                            	    x: 0,
	                            	    y: 0,
	                            	    x2: 0,
	                            	    y2: 1,
	                            	    colorStops: [{ 	  
	                            	        offset: 0, color: 'red' // 0% 处的颜色
	                            	    }, { 	
	                            	        offset: 1, color: 'red' // 100% 处的颜色
	                            	    }],
	                            	    globalCoord: false // 缺省为 false
	                            	},
//	                             borderWidth:0,		//描边的线宽
//	                             borderColor:'red',	//颜色
	                             opacity:0.6			//透明度
	                         },
	                        emphasis: {
	                            borderColor: '#1e90ff',
	                            borderWidth: 5,
	                            label: {
	                                show: true
	                            }
	                        }
	                    },
	                    data : markPoints
	                }
	            }]
	        });
	    });
}
function initGuanxi(){
	 var option = {
     	    // title: {
     	    //     text: "法院关系",
     	    //     top: "top",
     	    //     left: "left"
     	    // },
     	    tooltip: {show: true},
     	    legend: [
     	             {
     	        formatter: function(name) {
     	            return echarts.format.truncateText(name, 40, '14px Microsoft Yahei', '…');
     	        },
     	        tooltip: {
     	            show: true
     	        },
     	        selectedMode: 'false',
     	        bottom: 20
//     	        data:categorie
     	    }],
     	    animationDuration: 100,
     	    animationEasingUpdate: 'quinticInOut',
     	    series: [{
     	        type: 'graph',
     	        layout: 'force',
     	       draggable:true,
     	        force: {
     	            repulsion: 105
     	        },
     	      hoverAnimation:true,
     	      legendHoverLink : true,
     	        data: datas,
     	        //源节点和目标节点
     	        links:sourceTarget,
     	        //显示的节点
     	        categories:classification,
     	        focusNodeAdjacency: true, //相邻节点的突出显示
     	        roam: false,			  //是否开启鼠标缩放和平移漫游
     	        label: {
     	            normal: {
     	                show: true,    //是否显示节点标题
     	                position: 'top'
     	            }
     	        },
     	       itemStyle:{
     	    	   normal:{
     	    		  label: {
                          show: true,
                          textStyle: {
                              color: '#fff'
                          },
                          fontSize: '8px',
                          fontFamily:'Microsoft YaHei'
                      }
     	    	   }
     	    	  
     	       },
     	        lineStyle: {
     	            normal: {
     	            	//连接线的颜色
     	            	color: {
     	            	    type: 'linear',
     	            	    x: 0,
     	            	    y: 0,
     	            	    x2: 0,
     	            	    y2: 1,
     	            	    colorStops: [{
     	            	        offset: 0, color: 'red' // 0% 处的颜色
     	            	    }, {
     	            	        offset: 1, color: 'blue' // 100% 处的颜色
     	            	    }],
     	            	    globalCoord: false // 缺省为 false
     	            	},
     	                curveness: 0.3,
     	                type: "solid",
     	                width:2
     	            }
     	        }
     	    }]
     	};
    var myChart2 = ec.init(document.getElementById('chart_guanxi'));
    myChart2.setOption(option);
    //点击事件，点击进行相应的跳转
//    myChart2.on('click', function (params) {
//	    console.log(params);
//	    alert(params)
//	    window.location.href="http://127.0.0.1/cmp/views/open_court_point_ratio.html";
//	});
}
/**
 * 获取当前服务器的等级（高院，中院，基院）
 * @param city
 * @returns {string}
 */
function getLevel() {
    var level='';
    $.ajax({
        url : r.base_path + "getLevelForGoTo",
        type : 'post',
        async:false,
        dataType : "json",
        success : function(result) {
            level = result.data;
        }
    });
    return level;
}

